%% Performance Evaluation of EW UEP RLNC Coding Scheme
%
% Author: 12GR652
% Last modified: 26-04-2012
%
% Description:
% This script drives the ewrlnc simulation and saves results in a .mat
% for later plotting
%
% Speed:
%   - Note that ewrlnc(... ) is very very slow - compute with power!
%

%% Startup

% Clear workspace
close all
clear all
clc

% Setup matlab worker threads for parallel execution
% Utilizing 8 cores on js2.es.aau.dk crashed... trying 4 instead...

target_nb_worker_threads=2;
nb_worker_threads=matlabpool('size');

if nb_worker_threads==0
    matlabpool('open',target_nb_worker_threads)
    nb_worker_threads=matlabpool('size');
else 
    matlabpool('close','force')
    matlabpool('open',target_nb_worker_threads)
    nb_worker_threads=matlabpool('size');
end
    
% Note time (performance)
tic

% Prepare logfile
the_time_now=datestr(now);
filename_log=strcat(the_time_now,'_ewrlnc_log.txt');
filename_fig=strcat(the_time_now,'_ewrlnc_log.eps');
diary(filename_log)
diary on

% Begin diary
disp('Testing Expanding Windows RLNC');
disp('Nb of worker threads');
% disp(nb_worker_threads)


%% Parameters to test EWRLNC with:

m=1; % field parameter (2^m = field size), we shall use either 1 or 8
nb_sims=1; % simulations pr nb_transmissions
R1=0.7; % probability of layer 1
R2=0.3; % Probability of layer 2
% R3=0; % probability of layyer 3

I_pkts=32; % Nb of packets in I block
P1_pkts=32;% Nb of packets in P1 block
% P2_pkts=0; % Nb of packkets in P2 block
gen_size=I_pkts+P1_pkts; % Generation size

%% Save configuration to logfile

disp('Field parameter (2^m = field size), we shall use either 1 or 8');
disp(m);
disp('Simulations pr nb_transmissions');
disp(nb_sims);
disp('Probability of transmitting layer 1');
disp(R1);
disp('Probability of transmitting layer 2');
disp(R2);
% disp('Probability of transmitting layer 3');
% disp(R3);
disp('Nb of packets in I block');
disp(I_pkts);
disp('Nb of packets in P1 block');
disp(P1_pkts);
% disp('Nb of packets in P2 block');
% disp(P2_pkts);
disp('Generation size');
disp(gen_size);

%% Collect data

% The way data is saved is strange, parfor scripts executed from batch mode
% behaves differently from GUI execution .... strange.... It seems to be
% working correctly this way though...

L1_r=[];
L2_r=[];
% L3_r=[];
EEP_r=[];

% For pkt_loss=0 and increasing number of packets send, collect data
% Send until 3 x generation size has been send
parfor nbtransmissions = 1:(3*gen_size)
    nbt=nbtransmissions;
    [L1,L2]=ewrlnc_2layers(nb_sims,I_pkts,P1_pkts,m,nbtransmissions,R1,R2);
    L1_r=[L1_r;[L1 nbt]];
    L2_r=[L2_r;[L2 nbt]];

    
%     [~,~,P2]=ewrlnc(nb_sims,I_pkts,P1_pkts,P2_pkts,m,nbtransmissions,0,0,1);
%     EEP_r=[EEP_r;[P2 nbt]];

end

I_prob=L1_r./nb_sims;
P1_prob=L2_r./nb_sims;
% P2_prob=L3_r./nb_sims;
% EEP_prob=EEP_r./nb_sims;

% Save all data for plotter
save('ewrlnc_data'); 

% Finish data generation
disp('It took [s]')
disp(toc)
disp('This is the end...')
diary off

matlabpool('close','force') % close thread pool